package com.feige.algorithm;

import org.apache.shardingsphere.api.sharding.standard.RangeShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.RangeShardingValue;

import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Set;

/**
 * @author 抖音号：程序员飞哥
 * B站：https://space.bilibili.com/490711252
 * @version 1.0
 * @Title: Study512出品
 * @Description: 我们有一个学习网站：https://www.study512.com
 */
public class CustomRangeTableShardingAlgorithm implements RangeShardingAlgorithm<Long> {
    @Override
    public Collection<String> doSharding(Collection<String> tableNames, RangeShardingValue<Long> rangeShardingValue) {
        Set<String> result = new LinkedHashSet<>();
        // between 起始值
        Long start = rangeShardingValue.getValueRange().lowerEndpoint();
        // between 结束值
        Long end = rangeShardingValue.getValueRange().upperEndpoint();
        for (long i = start; i <= end; i++) {
            for (String tableName : tableNames) {
                if (tableName.endsWith(i % tableNames.size() + "")) {
                    result.add(tableName);
                }
            }
        }
        return result;
    }
}
